import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/train_teacher_degree_of_statisfaction_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../bean/bean_user.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/train_teacher_list_controller.dart';

///培训中的教师
class TrainTeacherPage extends StatefulWidget {
  final int trainId;

  const TrainTeacherPage({Key key, @required this.trainId}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainTeacherPageState();
  }
}

class _TrainTeacherPageState extends State<TrainTeacherPage>
    with SingleTickerProviderStateMixin {
  TrainTeacherListController _checkRecoderController;

  @override
  void initState() {
    super.initState();
    _checkRecoderController = Get.put(
      TrainTeacherListController(widget.trainId),
      tag: widget.trainId.toString(),
    );

    Future.delayed(Duration.zero, () async {
      await _checkRecoderController.requestListFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("培训教师"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.trainId.toString(),
        builder: (TrainTeacherListController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _checkRecoderController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    List<TrainTeacherEntity> _teacherList = _checkRecoderController.getList();

    if (_teacherList.isEmpty) {
      return Center(
        child: NoDataWidget(
          text: "无培训教师",
          clickCallBack: () {
            _checkRecoderController.requestListFunction(click: true);
          },
        ),
      );
    }

    return RefreshIndicator(
      onRefresh: () async {
        await _checkRecoderController.requestListFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: buildListView(),
    );
  }

  buildListView() {
    List<TrainTeacherEntity> _teacherList = _checkRecoderController.getList();
    return Container(
      child: GridView.builder(
        itemCount: _teacherList.length,
        itemBuilder: (BuildContext context, int index) {
          TrainTeacherEntity trainTeacherEntity = _teacherList[index];
          UserBean userBean = trainTeacherEntity.userBean;
          return InkWell(
            child: buildClipRRect(trainTeacherEntity, userBean, index),
            onTap: () {
              Get.to(
                () => TrainTeacherDegreeOfStatisFactionPage(
                    trainTeacherEntity: trainTeacherEntity),
              );
            },
          );
        },
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          //纵轴间距
          mainAxisSpacing: 2.0,
          //横轴间距
          crossAxisSpacing: 2.0,
          //子组件宽高长度比例
          childAspectRatio: 0.9, crossAxisCount: 3,
        ),
      ),
    );
  }

  buildClipRRect(
      TrainTeacherEntity trainTeacherEntity, UserBean userBean, int index) {
    if (userBean == null) {
      return SizedBox();
    }
    String realName = userBean.reamName;
    if (realName == null || realName.isEmpty) {
      return SizedBox();
    }
    String firstName = realName.substring(0, 1);
    return Container(
      child: Container(
        color: Colors.white,
        child: Column(
          children: [
            Card(
              child: Padding(
                padding: EdgeInsets.all(12),
                child: Text(
                  firstName,
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 33),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 12, top: 8),
              child: Row(
                children: [
                  Expanded(
                      child: Text(
                        "姓名:${userBean.reamName}",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      )),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 11),
              child: Row(
                children: [
                  Text(
                    "课程:${trainTeacherEntity.teacherBook}",
                    maxLines: 1,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
